﻿@echo off
:: ========== 配置区 ==========
set "PROJECT_NAME=Vue项目"
set "SHOW_SUCCESS_ANIMATION=1"
:: ============================

:: === 初始化设置 ===
setlocal enabledelayedexpansion
chcp 65001 > nul
title %PROJECT_NAME%清理工具 - UTF8模式
mode con: cols=90 lines=35
color 0E

:: === 主清理逻辑 ===
echo.
echo ===================================================
echo                %PROJECT_NAME% 缓存清理工具
echo ===================================================
echo.
echo [信息] 当前工作目录: %CD%
echo [操作] 开始清理项目...
echo.

:: 1. 删除 node_modules
call :delete_folder "node_modules" "依赖目录"

:: 2. 删除构建目录
call :delete_folder "dist" "构建输出"
call :delete_folder "build" "构建缓存"
call :delete_folder ".vite" "Vite缓存"

:: 3. 删除锁定文件 (保留package-lock.json删除)
call :delete_file "package-lock.json" "npm锁定文件"
call :delete_file "yarn.lock" "yarn锁定文件"
call :delete_file "pnpm-lock.yaml" "pnpm锁定文件"

:: 4. 清理编译缓存
call :delete_file ".eslintcache" "ESLint缓存"
call :delete_file ".babelrc" "Babel配置"
call :delete_file "tsconfig.tsbuildinfo" "TypeScript编译"
call :delete_file "*.log" "日志文件"

:: 5. 清理编辑器配置文件
call :delete_folder ".vscode" "VS Code配置"
call :delete_folder ".idea" "IntelliJ配置"
call :delete_file "*.code-workspace" "工作区文件"

:: 6. 清理npm缓存
echo [操作] 清理 npm 缓存...
npm cache clean --force >nul 2>&1
if !errorlevel! equ 0 (
    echo   ✔ npm缓存已清理
) else (
    echo   ✘ 清理npm缓存失败，错误码: !errorlevel!
)

:: === 完成报告 ===
echo.
echo ===================================================
echo [操作完成] %PROJECT_NAME% 缓存清理成功！
echo.
echo 请运行以下命令重新安装依赖：
echo   npm install
echo.
echo 或使用更快的pnpm:
echo   pnpm install
echo ===================================================
echo.

:: 添加成功动画
if %SHOW_SUCCESS_ANIMATION% equ 1 (
    echo 正在退出...
    ping -n 3 127.0.0.1 > nul
)

exit /b 0

:: === 子函数: 删除文件夹 ===
:delete_folder
set "folder_name=%~1"
set "display_name=%~2"

if exist "!folder_name!\" (
    echo [清理] 删除 !display_name!...
    rmdir /s /q "!folder_name!" 2>nul
    if !errorlevel! equ 0 (
        echo   ✔ 已删除 !display_name! 目录
    ) else (
        echo   ✘ 删除失败: !display_name! (可能被占用)
    )
) else (
    echo   ℹ !display_name! 不存在，跳过
)
goto :eof

:: === 子函数: 删除文件 ===
:delete_file
set "file_pattern=%~1"
set "display_name=%~2"

set "found=0"
for %%f in ("!file_pattern!") do (
    if exist "%%f" (
        set "found=1"
        echo [清理] 删除 !display_name! (%%f)...
        del /q "%%f" 2>nul
        if !errorlevel! equ 0 (
            echo   ✔ 已删除: %%f
        ) else (
            echo   ✘ 删除失败: %%f (可能被占用)
        )
    )
)

if !found! equ 0 (
    echo   ℹ !display_name! 未找到，跳过
)
goto :eof